﻿using LogisticsTrackingWebApi.Models;
using LogisticsTrackingWebApi.Services;

namespace LogisticsTrackingWebApi.ServicesImpl
{

    /// <summary>
    /// 物流查询服务实现
    /// </summary>
    public class LogisticsService : ILogisticsService
    {
        // 模拟支持的物流公司
        private readonly List<LogisticsCompany> _supportedCompanies = new List<LogisticsCompany>
        {
            new LogisticsCompany { Code = "SF", Name = "顺丰速运", OrderNumberFormat = "12位数字" },
            new LogisticsCompany { Code = "YD", Name = "韵达快递", OrderNumberFormat = "13位数字" },
            new LogisticsCompany { Code = "ZT", Name = "中通快递", OrderNumberFormat = "12位数字" },
            new LogisticsCompany { Code = "ST", Name = "申通快递", OrderNumberFormat = "12位数字" }
        };

        /// <summary>
        /// 获取支持的物流公司列表
        /// </summary>
        public async Task<List<LogisticsCompany>> GetSupportedCompaniesAsync()
        {
            // 模拟异步操作
            await Task.Delay(100);
            return _supportedCompanies.ToList();
        }

        /// <summary>
        /// 根据订单号查询物流信息
        /// </summary>
        public async Task<LogisticsOrder> GetOrderInfoAsync(string orderNumber, string? companyCode = null)
        {
            // 模拟异步查询操作
            await Task.Delay(300);

            // 如果未指定公司代码，尝试根据订单号格式猜测
            if (string.IsNullOrEmpty(companyCode))
            {
                companyCode = GuessCompanyCodeByOrderNumber(orderNumber);
            }

            var company = _supportedCompanies.FirstOrDefault(c => c.Code == companyCode);
            if (company == null)
            {
                throw new KeyNotFoundException("不支持的物流公司");
            }

            // 模拟不同公司的订单信息
            return company.Code switch
            {
                "SF" => GenerateSFOrder(orderNumber, company),
                "YD" => GenerateYDOrder(orderNumber, company),
                "ZT" => GenerateZTOrder(orderNumber, company),
                "ST" => GenerateSTOrder(orderNumber, company),
                _ => throw new KeyNotFoundException("不支持的物流公司")
            };
        }

        // 根据订单号猜测公司代码
        private string? GuessCompanyCodeByOrderNumber(string orderNumber)
        {
            // 简单的格式判断逻辑，实际应用中可能更复杂
            if (orderNumber.Length == 12 && long.TryParse(orderNumber, out _))
            {
                // 12位数字可能是顺丰、中通或申通
                return "SF"; // 这里简单返回第一个匹配的，实际可能需要更复杂的判断
            }
            else if (orderNumber.Length == 13 && long.TryParse(orderNumber, out _))
            {
                // 13位数字可能是韵达
                return "YD";
            }
            return null;
        }

        // 生成顺丰订单模拟数据
        private LogisticsOrder GenerateSFOrder(string orderNumber, LogisticsCompany company)
        {
            return new LogisticsOrder
            {
                OrderNumber = orderNumber,
                LogisticsCompanyCode = company.Code,
                LogisticsCompanyName = company.Name,
                Sender = "张三",
                Receiver = "李四",
                Status = "运输中",
                CreateTime = DateTime.Now.AddDays(-2),
                EstimatedDeliveryTime = DateTime.Now.AddDays(1),
                Tracks = new List<LogisticsTrack>
                {
                    new LogisticsTrack { Time = DateTime.Now.AddDays(-2), Location = "北京市朝阳区", Description = "快递已揽收" },
                    new LogisticsTrack { Time = DateTime.Now.AddDays(-1.5), Location = "北京市海淀区", Description = "快递已发出" },
                    new LogisticsTrack { Time = DateTime.Now.AddDays(-1), Location = "上海市松江区", Description = "快递已到达中转站" },
                    new LogisticsTrack { Time = DateTime.Now.AddHours(-6), Location = "上海市静安区", Description = "正在派送中" }
                }
            };
        }

        // 生成韵达订单模拟数据
        private LogisticsOrder GenerateYDOrder(string orderNumber, LogisticsCompany company)
        {
            return new LogisticsOrder
            {
                OrderNumber = orderNumber,
                LogisticsCompanyCode = company.Code,
                LogisticsCompanyName = company.Name,
                Sender = "王五",
                Receiver = "赵六",
                Status = "已签收",
                CreateTime = DateTime.Now.AddDays(-5),
                EstimatedDeliveryTime = DateTime.Now.AddDays(-1),
                Tracks = new List<LogisticsTrack>
                {
                    new LogisticsTrack { Time = DateTime.Now.AddDays(-5), Location = "广州市天河区", Description = "快递已揽收" },
                    new LogisticsTrack { Time = DateTime.Now.AddDays(-4), Location = "广州市白云区", Description = "快递已发出" },
                    new LogisticsTrack { Time = DateTime.Now.AddDays(-3), Location = "杭州市西湖区", Description = "快递已到达中转站" },
                    new LogisticsTrack { Time = DateTime.Now.AddDays(-2), Location = "杭州市滨江区", Description = "正在派送中" },
                    new LogisticsTrack { Time = DateTime.Now.AddDays(-1), Location = "杭州市滨江区", Description = "快递已签收，签收人：前台" }
                }
            };
        }

        // 生成中通订单模拟数据
        private LogisticsOrder GenerateZTOrder(string orderNumber, LogisticsCompany company)
        {
            return new LogisticsOrder
            {
                OrderNumber = orderNumber,
                LogisticsCompanyCode = company.Code,
                LogisticsCompanyName = company.Name,
                Sender = "钱七",
                Receiver = "孙八",
                Status = "运输中",
                CreateTime = DateTime.Now.AddDays(-3),
                EstimatedDeliveryTime = DateTime.Now.AddDays(2),
                Tracks = new List<LogisticsTrack>
                {
                    new LogisticsTrack { Time = DateTime.Now.AddDays(-3), Location = "深圳市福田区", Description = "快递已揽收" },
                    new LogisticsTrack { Time = DateTime.Now.AddDays(-2.5), Location = "深圳市南山区", Description = "快递已发出" },
                    new LogisticsTrack { Time = DateTime.Now.AddDays(-2), Location = "武汉市江汉区", Description = "快递已到达中转站" },
                    new LogisticsTrack { Time = DateTime.Now.AddHours(-12), Location = "郑州市金水区", Description = "快递已到达中转站" }
                }
            };
        }

        // 生成申通订单模拟数据
        private LogisticsOrder GenerateSTOrder(string orderNumber, LogisticsCompany company)
        {
            return new LogisticsOrder
            {
                OrderNumber = orderNumber,
                LogisticsCompanyCode = company.Code,
                LogisticsCompanyName = company.Name,
                Sender = "周九",
                Receiver = "吴十",
                Status = "已发出",
                CreateTime = DateTime.Now.AddDays(-1),
                EstimatedDeliveryTime = DateTime.Now.AddDays(3),
                Tracks = new List<LogisticsTrack>
                {
                    new LogisticsTrack { Time = DateTime.Now.AddDays(-1), Location = "成都市锦江区", Description = "快递已揽收" },
                    new LogisticsTrack { Time = DateTime.Now.AddHours(-18), Location = "成都市武侯区", Description = "快递已发出" },
                    new LogisticsTrack { Time = DateTime.Now.AddHours(-6), Location = "重庆市渝中区", Description = "快递已到达中转站" }
                }
            };
        }
    }
}
